package com.hotel.company.service;

import com.hotel.comm.bean.DubboReply;
import com.hotel.comm.bean.IReply;
import com.hotel.comm.bean.company.EjobCompany;
import com.hotel.comm.service.company.ICompanyService;
import com.hotel.comm.vo.company.EjobCompanyVo;
import com.hotel.company.Constants;
import com.hotel.company.repository.IEjobCompanyRepository;
import com.hotel.core.utils.DateUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CompanyServiceImpl implements ICompanyService {
    @Autowired
    private IEjobCompanyRepository ejobCompanyRepository;

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public IReply getById(Long companyId) {
        try {
            EjobCompany company = this.ejobCompanyRepository.findOne(companyId);
            return new DubboReply(company);
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_COMPANY_GET_BY_ID, Constants.REPLY_MSG_COMPANY_GET_BY_ID);
        }
    }

    @Override
    public IReply save(EjobCompany ejobCompany) {
        try {
            ejobCompany.setCreateDate(DateUtils.getCurrentDate());
            ejobCompany.setUpdateDate(DateUtils.getCurrentDate());
            ejobCompany.setRefleshDate(DateUtils.getCurrentDate());
            EjobCompany company = this.ejobCompanyRepository.save(ejobCompany);
            return new DubboReply(company);
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_SAVE_COMPANY, Constants.REPLY_MSG_SAVE_COMPANY);
        }
    }

    @Override
    public IReply update(EjobCompanyVo ejobCompanyVo) {
        try {
            EjobCompany ejobCompany = ejobCompanyRepository.findOne(ejobCompanyVo.getId());
            BeanUtils.copyProperties(ejobCompany, ejobCompanyVo);

            ejobCompany.setUpdateDate(DateUtils.getCurrentDate());
            EjobCompany company = this.ejobCompanyRepository.save(ejobCompany);
            return new DubboReply(company);
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_SAVE_COMPANY, Constants.REPLY_MSG_SAVE_COMPANY);
        }
    }

    @Override
    public IReply delete(Long companyId) {
        try {
            this.ejobCompanyRepository.delete(companyId);
            return new DubboReply(companyId);
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_DELETE_COMPANY, Constants.REPLY_MSG_DELETE_COMPANY);
        }
    }

    @Override
    public IReply findByCompanyNameLike(String companyName) {
        try {
            List<EjobCompany> ejobCompanyList = ejobCompanyRepository.findByCompanyNameLike(companyName);
            return new DubboReply(ejobCompanyList);
        } catch (Exception e) {
            return new DubboReply(Constants.REPLY_CODE_FIND_LIKE_COMPANY_NAME, Constants.REPLY_MSG_FIND_LIKE_COMPANY_NAME);
        }
    }
}
